package com.sentiment.parser.news;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sentiment.database.bean.CrawlInfo;
import com.sentiment.database.dbUtil.DbUtil;
import com.sentiment.jsparser.PhantomjsCore;
import com.sentiment.nlpalgo.bean.Double2;
import com.sentiment.nlpalgo.bean.Keywords;
import com.sentiment.nlpalgo.method.AlgoHttp;
import com.sentiment.tools.Format;
import com.sentiment.webcollector.model.Page;

public class PeopleNews implements NewsParser {
	private static final int MINLENGTH = 5;
	private static final Logger LOG = LoggerFactory.getLogger(IfengNews.class);
	private Page page;

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	private Document doc;
	private AlgoHttp algo = new AlgoHttp();

	public static final String REGEX = ".*\\.ifeng\\..*";

	public PeopleNews(Page page) {
		if (page != null) {
			this.page = page;
			LOG.info("Phantomjs解析动态页面完成 : " + this.REGEX);
		} else
			LOG.error("page is empty ! ");
	}

	@Override
	public String getTitle() {
		// TODO Auto-generated method stub
		Element ele = doc.getElementById("p_title");
		return ele.text();
	}

	@Override
	public Date getReleaseTime() {
		// TODO Auto-generated method stub
		Element ele = doc.getElementById("p_publishtime");
		String src = ele.text().replaceAll("[^0-9]", "_");
		System.out.println(src);
		return Format.string2Date(src, 6);
	}

	@Override
	public List<String> getText() {
		// TODO Auto-generated method stub
		// Element ele = doc.getElementById("p_content");
		Element ele = doc.getElementsByAttributeValue("class", "clearfix").first();
		List<String> li = new ArrayList<String>();
		Elements pElems = ele.getElementsByTag("p");
		Iterator<Element> it = pElems.iterator();
		while (it.hasNext()) {
			String s = it.next().text();
			if (s.length() > MINLENGTH)
				li.add(s);
		}
		return li;
	}

	@Override
	public String getPublisher() {
		// TODO Auto-generated method stub
		Element ele = doc.getElementById("p_origin");
		// Element ele = doc.getElementsByAttributeValue("target",
		// "_blank").first();
		Elements aElems = ele.getElementsByTag("a");
		Iterator<Element> it = aElems.iterator();
		return it.next().text();
	}

	private int[] CommentAndPageView() {
		int[] i = { 0, 0 };
		int index = 0;
		Element ele = doc.getElementById("all_link");
		i[0] = Integer.parseInt(ele.text().replaceAll("[^0-9]", ""));
		return i;
	}

	@Override
	public int getComment() {
		// TODO Auto-generated method stub
		int[] i = CommentAndPageView();
		return i[0];
	}

	@Override
	public int getPageView() {
		// TODO Auto-generated method stub
		int[] i = CommentAndPageView();
		return i[1];
	}

	@Override
	public List<Double2> getSentiment(List<String> text) {
		// TODO Auto-generated method stub
		ArrayList<Double2> list = algo.sentiment(text, AlgoHttp.SENTIMENT_NEWS);
		return list;
	}

	@Override
	public Double2 getSentiment(String text) {
		// TODO Auto-generated method stub
		List<String> list = new ArrayList<String>();
		list.add(text);
		ArrayList<Double2> result = algo.sentiment(list, AlgoHttp.SENTIMENT_NEWS);
		return result.get(0);
	}

	@Override
	public List<Keywords> getKeywords(List<String> text) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Keywords getKeywords(String text) {
		// TODO Auto-generated method stub
		return null;
	}

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		Page page = new Page();
		StringBuilder sb = new StringBuilder();
		BufferedReader br = new BufferedReader(new FileReader("C:/Users/冉冉/Desktop/PeopleNews2.html"));
		String temp = null;
		while ((temp = br.readLine()) != null) {
			sb.append(temp);
		}
		page.setDoc(Jsoup.parse(sb.toString()));
		PeopleNews n = new PeopleNews(page);
		System.out.println(n.getTitle());
		Date d = n.getReleaseTime();
		System.out.println(Format.date2String(d, 6));
		sb.delete(0, sb.length());
		List<String> li = n.getText();
		for (String s : li) {
			System.out.println(s);
			sb.append(s);
		}

		System.out.println(sb);
		System.out.println(n.getPublisher());
		System.out.println(n.getComment());
		System.out.println(n.getPageView());
	}

}
